package com.zzj.hotel.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.rowset.serial.SerialBlob;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;

import java.sql.Blob;
import java.sql.SQLException;

import com.zzj.hotel.core.entity.Article;
import com.zzj.hotel.core.entity.Message;
import com.zzj.hotel.core.entity.User;
import com.zzj.hotel.core.utils.AjaxUtil;
import com.zzj.hotel.core.utils.IdManager;
import com.zzj.hotel.core.utils.PageUtil;
import com.zzj.hotel.service.ArticleService;
import com.zzj.hotel.service.MessageService;

@Controller
@RequestMapping(value={"/article"})
public class ArticleController {
	@Autowired ArticleService articleService;
	@Autowired MessageService messageService;
	@RequestMapping(value={"/list"}, method=RequestMethod.POST)
	public void orign(HttpServletRequest request, HttpServletResponse response){
		String classifyId = request.getParameter("classifyId");
		List<Article> list = articleService.findList(classifyId);
		Map<String,Object> map = new HashMap<String,Object>();
		for(Article Article:list){
			map.put(Article.getArticle_id(), Article);
		}
		String jsonResult = JSON.toJSONString(map);
		AjaxUtil.renderData(response,jsonResult);
	}
	@RequestMapping(value={"/lists"}, method=RequestMethod.POST)
	public void lists(HttpServletRequest request, HttpServletResponse response){
		String classifyId = request.getParameter("classifyId");
		String page_temp = request.getParameter("page");
		String hql = "select count(*) from article t where t.article_classifyId='"+classifyId+"'";
		Long count = articleService.getCount(hql);
		PageUtil pageUtil = new PageUtil();
		int page = 1;
		if(page_temp != null && !"".equals(page_temp.trim())){
			page = Integer.parseInt(page_temp);
		}
		String str = count.toString();
		Integer i2 = new Integer(str);
		int i3 = i2;
		pageUtil.setPage(page);
		pageUtil.setTotalCount(i3);
		pageUtil.setPageSize();
		pageUtil.setTotalPage();
		pageUtil.setStart();
		pageUtil.setEnd();
		pageUtil.setNextPage();
		pageUtil.setLastPage();
		List<Article> list = articleService.findList(classifyId,pageUtil.getStart(),pageUtil.getEnd(),Article.ARTICLE_CREATETIME);
		Map<String,Object> map = new HashMap<String,Object>();
		for(Article Article:list){
			map.put(Article.getArticle_id(), Article);
		}
		map.put("page", pageUtil);
		String jsonResult = JSON.toJSONString(map);
		AjaxUtil.renderData(response,jsonResult);
	}


	@RequestMapping(value={"/update"}, method=RequestMethod.POST)
	public void update(HttpServletRequest request, HttpServletResponse response){
		String ArticleId = request.getParameter("ArticleId");
		String Article_pid = request.getParameter("Article_pid");
		String ArticleName = request.getParameter("ArticleName");
		Article Article1 = articleService.findById(ArticleId);
	}

	@RequestMapping(value={"/delete"}, method=RequestMethod.POST)
	public void delete(HttpServletRequest request, HttpServletResponse response){
		String articleId = request.getParameter("articleId");
		articleService.deleteArticle(articleId);
		AjaxUtil.renderData(response,"true");
	}

	@RequestMapping(value={"/toadd"}, method=RequestMethod.GET)
	public ModelAndView toadd(HttpServletRequest request, HttpServletResponse response,Model model){
		String classifyId = request.getParameter("classifyId");
		String articleId = request.getParameter("articleId");
		Article article = new Article();
		if(articleId != null && !"".equals(articleId.trim())){
			article = articleService.findById(articleId);
		}
		if("null".equals(articleId)){
			request.getSession().removeAttribute("articleTitle");
			request.getSession().removeAttribute("articleContent");
			request.getSession().removeAttribute("articleId");
		}
		if(article != null){
			Blob content_temp = article.getArticle_content();
			String content = "";
			try{  
				content = new String(content_temp.getBytes((long)1, (int)content_temp.length()));  
			} catch(SQLException e) {  
				e.printStackTrace();  
			}  
			request.getSession().setAttribute("articleTitle", article.getArticle_title());
			request.getSession().setAttribute("articleContent", content);
			request.getSession().setAttribute("articleId", articleId);
		}
		request.getSession().setAttribute("classifyId", classifyId);
		return new ModelAndView("../view/FCKeditor/edit");
	}
	@RequestMapping(value={"/show"})
	public void show(HttpServletRequest request, HttpServletResponse response){
		String articleId = request.getParameter("articleId");
		Article article = new Article();
		List<Message> tempList = messageService.findList(articleId);
		if(articleId != null && !"".equals(articleId.trim())){
			article = articleService.findById(articleId);
		}
		Map<String,Object> map = new HashMap<String,Object>();

		String content = "";    
		if(article != null){
			Blob content_temp = article.getArticle_content();
			try{  
				content = new String(content_temp.getBytes((long)1, (int)content_temp.length()));  
			} catch(SQLException e) {  
				e.printStackTrace();  
			} 
		}
		for (Message message:tempList) {
			map.put(message.getMsg_id(), message);
		}
		map.put("article", article);
		map.put("content", content);
		String jsonResult = JSON.toJSONString(map);
		AjaxUtil.renderData(response,jsonResult);
	}

	@RequestMapping(value={"/add"}, method=RequestMethod.POST)
	public void add(HttpServletRequest request, HttpServletResponse response){
		String article_title = request.getParameter("article_title");
		String article_content = request.getParameter("article_content");
		Blob content = (Blob) Hibernate.createBlob(article_content.getBytes());
		String classifyId = request.getParameter("classifyId");
		String articleId = request.getParameter("articleId");
		if(articleId != null && !"".equals(articleId)){
			Article article = articleService.findById(articleId);
			article.setArticle_title(article_title);
			article.setArticle_content(content);
			articleService.updateArticle(article);
			AjaxUtil.renderData(response,"update");
		}else{
			Article article1 = new Article();
			article1.setArticle_id(IdManager.getId());
			article1.setArticle_author(request.getSession().getAttribute("userName").toString());
			article1.setArticle_authorId(request.getSession().getAttribute("userId").toString());
			article1.setArticle_classifyId(classifyId);
			article1.setArticle_content(content);
			article1.setArticle_createTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
			article1.setArticle_title(article_title);
			articleService.addArticle(article1);
			AjaxUtil.renderData(response,"add");
		}

	}
	@RequestMapping(value={"/addMessage"}, method=RequestMethod.POST)
	public String aaddMessagedd(HttpServletRequest request, HttpServletResponse response){
		String articleId = request.getParameter("articleId");
		String msg_content = request.getParameter("msg_content");
		String msg_id = IdManager.getId();
		Object tempName = request.getSession().getAttribute("userName");
		Object tempId = request.getSession().getAttribute("userId");
		String author = "游客";
		String authorId = "";
		if(tempName != null){
			author = tempName.toString();
		}
		if(tempId != null){
			authorId = tempId.toString();
		}
		Message message = new Message();
		message.setMsg_articleId(articleId);
		message.setMsg_author(author);
		message.setMsg_authorId(authorId);
		message.setMsg_id(msg_id);
		message.setMsg_content(msg_content);
		message.setMsg_createTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		messageService.addMessage(message);
		return "redirect:show?articleId="+articleId;

	}
	@RequestMapping(value={"/initIndex"}, method=RequestMethod.POST)
	public void initIndex(HttpServletRequest request, HttpServletResponse response){
		List<Article> tempList = articleService.findList(0, 30, Article.ARTICLE_CREATETIME);
		Map<String,Object> map = new HashMap<String,Object>();
		for(Article article : tempList){
			map.put(article.getArticle_id(), article);
		}
		String jsonResult = JSON.toJSONString(map);
		AjaxUtil.renderData(response,jsonResult);
	}
	public static final int PAFESIZE = 10;

}
